<?php

class admin_model_system_menu extends model
{

	/**
	 * 保存菜单为静态 php 文件以提高执行效率
	 * @param int $group_id
	 */
	public function update($group_id)
	{
		$group = bone::get_admin_table('system', 'menu_group');
		$group->load($group_id);

		$menu_list = $this->get_menus( $group_id );

		$s  = '<?php' . "\r\n";
		$s .= 'class menu_'.$group->class_name.' extends menu' . "\r\n";
		$s .= '{' . "\r\n";
		$s .= '  public function __construct()' . "\r\n";
		$s .= '  {' . "\r\n";
		foreach($menu_list as $menu)
		{
			$s .= '    $this->add_menu('.$menu->id.', '.$menu->parent_id.', \''.$menu->name.'\', \''.$menu->url.'\', \''.$menu->target.'\');'."\r\n";
		}
		$s .= '  }' . "\r\n";
		$s .= '}' . "\r\n";
		$s .= '?>';

		file_put_contents( BONE_ROOT.DS.'menus'.DS.$group->class_name.'.php', $s );
	}
    
    
    
	/**
	 * 获取菜单项列表
	 * @param int $group_id 菜单组编号
	 */
	public function get_menus( $group_id )
	{
		$db = bone::get_db();
		return $db->get_objs( 'SELECT * FROM `bone_system_menu` WHERE `group_id`='.$group_id.' ORDER BY `ordering` ASC' );
	}


	/**
	 * 删除菜单
	 * @param int $menu_id 菜单编号
	 */
	public function delete($menu_id)
	{
		$db = bone::get_db();
		$db->execute( 'UPDATE `bone_system_menu` SET `parent_id`=0 WHERE `parent_id`='.$menu_id );
		$db->execute( 'DELETE FROM `bone_system_menu` WHERE `id`='.$menu_id );
		return true;
	}
	
	

}
?>